package admin

import (
	"fmt"
	"goProject/db"
	"goProject/table"
	"net/http"

	"gitee.com/ruige_fun/util/rlog"

	"gitee.com/ruige_fun/util/std"
	"github.com/kataras/iris/v12"
)

// getAllProduct 获取全部的商品列表
func getAllProduct(c iris.Context) {
	var result []table.Product
	db.MySQL.Find(&result)
	c.JSON(std.NewResp(http.StatusOK, "", result, nil))
}

type pSettingProduct struct {
	Key  string  `json:"key" validate:"required,min=1"` //商品ke
	Vip1 float64 `json:"vip_1"`                         //vip1的单价
	Vip2 float64 `json:"vip_2"`                         //vip2的单价
	Vip3 float64 `json:"vip_3"`                         //vip3的单价
	Vip4 float64 `json:"vip_4"`                         //vip4的单价
	Vip5 float64 `json:"vip_5"`                         //vip5的单价
	Vip6 float64 `json:"vip_6"`                         //vip6的单价
}

// settingProduct 设置商品的价格
func settingProduct(c iris.Context) {
	var p pSettingProduct
	err := c.ReadJSON(&p)
	if err != nil {
		c.JSON(std.NewResp(http.StatusBadRequest, "参数校验失败", nil, nil))
		return
	}
	var product table.Product
	err = db.MySQL.Where("`key` = ?", p.Key).First(&product).Error
	if err != nil {
		c.JSON(std.NewResp(http.StatusBadRequest, "商品不存在或已删除", nil, nil))
		return
	}
	temp := product
	product.Vip1 = p.Vip1
	product.Vip2 = p.Vip2
	product.Vip3 = p.Vip3
	product.Vip4 = p.Vip4
	product.Vip5 = p.Vip5
	product.Vip6 = p.Vip6
	err = db.MySQL.Select("Vip1", "Vip2", "Vip3", "Vip4", "Vip5", "Vip6").Where("`key` = ?", product.Key).Updates(&product).Error
	if err != nil {
		c.JSON(std.NewResp(http.StatusBadRequest, "价格更新失败", nil, nil))
		return
	}
	rlog.Success("修改商品价格：管理员", c.GetID(), "修改了商品单价", fmt.Sprintf("%+v", product), "原来的单价", fmt.Sprintf("%+v", temp))
	c.JSON(std.NewResp(http.StatusOK, "", nil, nil))
}
